var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];

var moreData = "";

getAllDCdateBuType()
function getAllDCdateBuType() {
    var params={
        areaId:'',
        level:102,
        addressCode:''
    };
    $.ajax({
        url: root.sroreinter.getAreaCityMap,//root.sroreinter.getAreaCityMap,
        cache: false, //禁用缓存
        type: "POST",
        data:params,
        success: function(result){
            if(result.errorCode== 0){
                console.log('我是主数据区域设置')
                console.log(result.data)
                moreData = result;
                goodsList(moreData);
            }else {
                layer.msg(result.errorMessage, {icon: 5,time:1000});
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

function goodsList(dataList){
    console.log(dataList)
    if(dataSource.length>0){
        dataSource.splice(0, dataSource.length); //清空数组
    }

    if(dataList.data != null){
        $.each(dataList.data, function(i, field) {
            var rowData = new Array();
            rowData.push(field.areaName);
            rowData.push(field.areaCityDto);
            dataSource.push(rowData);
            rowData.push(field.areaCode);
            rowData.push(field.areaId);

        });
    }

    rangeDataTable(dataSource);
    function rangeDataTable(dataSource) {
        var dttable = $('#warehouseTable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#warehouseTable").DataTable({
            "autoWidth": false,
            "ordering": true,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [{
                "visible": true,
                "targets": 0
            },
                {
                    "render": function(data, type, row, meta) {
                        var dataAll = row[1];
                        var ulTop = '<ul class="radius text-l" id="ulList">';
                        var list="";
                        var ulfoot ='</ul>';
                        for(var i=0;i<dataAll.length;i++) {
                        list += '<li class="f-l ml-10 liList" id="' + dataAll[i].addressCode + '"><span class="name">' + dataAll[i].addressName + '<a id="'+dataAll[i].addressCode+'" class=" 130" onclick="more(this,'+row[3]+')"> 更多</a></span></li>'
                        }
                        ulTop += list + ulfoot;
                        return ulTop;
                    },
                    "targets": 1
                },
            ]
        });
        //权限控制函数
        power()
    };
}

function more(e,areaId) {
    var params={
        areaId:areaId,
        level:102,
        addressCode:$(e).attr('id')
    };
    var morecityname='';
    $.ajax({
        url: root.sroreinter.getAreaCityMap,
        cache: false, //禁用缓存
        type: "POST",
        data:params,
        success: function(result){
            if(result.errorCode== 0){
                console.log('我是更多主数据区域设置')
                console.log(result.data)
             
                var morecity='';
                if(result.data.length >0){
                    $.each(result.data,function(i,item){
                       
                        $.each(item.areaCityDto,function(j,ite){
                            morecityname+=ite.addressName +','
                        })
                    })
                    layer.confirm(morecityname, {
                        btn: ['返回'] //按钮
                    }, function(index,layero){
                        layer.close(index);
                        //确定执行方法
                    });
                }
            
            }else {
                layer.msg(result.errorMessage, {icon: 5,time:1000});
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });


    
}


var dataSource2 = new Array();
//table的数据源
var table; //table对象
dataSource2 = [];
console.log('我是发货仓法护公司')
getAllDCdateBuType2()
function getAllDCdateBuType2() {
    $.ajax({
        url:root.sroreinter.getWarehouseAndCompanyAndProvinceCode,
        cache: false, //禁用缓存
        type: "POST",
        success: function(result){
            if(result.errorCode=="0"){
                goodsList2(result);
            }else {
                layer.msg(result.errorMessage, {icon: 5,time:1000});
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

function goodsList2(dataList){
    console.log(dataList)
    if(dataSource2.length>0){
        dataSource2.splice(0, dataSource2.length); //清空数组
    }

    if(dataList.data != null){
        $.each(dataList.data, function(i, field) {
            var rowData = new Array();
            rowData.push(field.warehouseName);
            rowData.push(field.selfMentionPointName);
            rowData.push(field.provinceName);
            dataSource.push(rowData);

        });
    }

    rangeDataTable2(dataSource);
    function rangeDataTable2(dataSource) {
        var dttable = $('#delivery-warehousecitybox').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#delivery-warehousecitybox").DataTable({
            "autoWidth": false,
            "ordering": true,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [{
                "visible": true,
                "targets": 0
            },
                {
                    "render": function(data, type, row, meta) {
						var val = '<span><td>' + row[0] + '</td></span>';
						return val;
					},
					"targets":0
                },
                {
					"render": function(data, type, row, meta) {
						var val = '<span><td>' + row[1] + '</td></span>';
						return val;
					},
					"targets": 1
				},
                {
					"render": function(data, type, row, meta) {
						var val = '<span><td>' + row[2] + '</td></span>';
						return val;
					},
					"targets": 2
				},
            ]
        });
        //权限控制函数
        power()
    };
}
